﻿Public Class ConsoleCfgForm

    Private config As Configuration.Configuration
    Private _settings As Configuration.KeyValueConfigurationCollection

    Private Sub ConsoleCfgForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.CenterToParent()

        config = System.Configuration.ConfigurationManager.OpenExeConfiguration(Configuration.ConfigurationUserLevel.None)
        If config IsNot Nothing Then
            _settings = config.AppSettings.Settings
            Me.dbaddr.Text = _settings("Sqladdr").Value
            Me.txtSqlDataSource.Text = _settings("SqlDataSource").Value
            Me.txtSqlUserName.Text = _settings("SqlUserName").Value
            Me.txtSqlUsrPwd.Text = "123456" 'IIf(_settings("SqlUsrPwd").Value.Length > 0, "123456", String.Empty)
        Else
            msg.ShowError("未找到控制台配置文件")

        End If
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        _settings("Sqladdr").Value = Me.dbaddr.Text
        _settings("SqlDataSource").Value = Me.txtSqlDataSource.Text
        _settings("SqlUserName").Value = Me.txtSqlUserName.Text
        If Me.txtSqlUsrPwd.Text <> "123456" Then _settings("SqlUsrPwd").Value = Me.txtSqlUsrPwd.Text

        Dim provider As String = "DataProtectionConfigurationProvider"
        Dim cfgString As Configuration.ConfigurationSection = config.AppSettings
        cfgString.SectionInformation.ProtectSection(provider)
        cfgString.SectionInformation.ForceSave = True
        config.Save(Configuration.ConfigurationSaveMode.Full)

        Me.DialogResult = Windows.Forms.DialogResult.OK

        DbInit()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub txtHbStartTime_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        e.Handled = True
        If (e.KeyChar >= "0" AndAlso e.KeyChar <= "9") OrElse (Asc(e.KeyChar) = Keys.Back) Then
            e.Handled = False
        End If
    End Sub

    Private Sub txtSqlDataSource_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSqlDataSource.KeyPress, txtSqlUsrPwd.KeyPress, txtSqlUserName.KeyPress
        e.Handled = True
        If System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar, "^[a-z]|[A-Z]|[0-9]|_$") OrElse (Asc(e.KeyChar) = Keys.Back) Then
            e.Handled = False
        End If
    End Sub

    Private Sub DbInit()
        Dim config As Configuration.Configuration
        Dim _settings As Configuration.KeyValueConfigurationCollection
        config = System.Configuration.ConfigurationManager.OpenExeConfiguration(Configuration.ConfigurationUserLevel.None)
        If config IsNot Nothing Then
            _settings = config.AppSettings.Settings
            SqlHelper.SetDatabaseFileName(_settings("Sqladdr").Value, _settings("SqlDataSource").Value, _settings("SqlUserName").Value, _settings("SqlUsrPwd").Value)
        End If
    End Sub
End Class